﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dqs_BLL;
using Dqs_DAL;
using Model;
using Sunny.UI;

namespace Dqs_UI
{
	public partial class frmAdmin_Teacher : UIForm
	{
		public frmAdmin_Teacher()
		{
			InitializeComponent();
		}

		Teacher_BLL Teacher_BLL = new Teacher_BLL();
		Course_BLL course_BLL = new Course_BLL();

		private void dgvStudentInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{

		}

		private void frmAdmin_Teacher_Load(object sender, EventArgs e)
		{

			//加载课程
			List<CourseInfo> list = course_BLL.GetCourses();
			dgvCourseInfo.DataSource = list;

			//CourseInfo[] courseInfos = list.ToArray();
			//List<CourseInfo> list2 = new List<CourseInfo>();
			//foreach (var item in courseInfos)
			//{
			//	list2.Add(item);
			//}
			//list2.Insert(0, new CourseInfo() { CourseName = "--请选择--", Id = -1 });

			////复制数据源，避免下拉框和DGV出现联动问题


			////把list再次赋值给cmbCategory
			//cmbCourseList.DisplayMember = "CourseName";
			//cmbCourseList.ValueMember = "Id";
			//cmbCourseList.DataSource = list2;

			//默认选择第一行
			this.dgvCourseInfo.Rows[0].Selected = true;

			//加载信息
			LoadTeacherInfo();
		}

		private void LoadTeacherInfo()
		{
			List<TeacherInfo> list = Teacher_BLL.GetTeachers();
			dgvStudentInfo.DataSource = list;
		}

		private void btnInsertCourseInfo_Click(object sender, EventArgs e)
		{
			frmAddCourse frmAddCourse = new frmAddCourse();
			frmAddCourse.FormClosed += FrmAddCourse_FormClosed; ;
			frmAddCourse.ShowDialog();
		}

		private void FrmAddCourse_FormClosed(object? sender, FormClosedEventArgs e)
		{
			frmAdmin_Teacher_Load(this, e);
		}

		private void btnUpdateCourseInfo_Click(object sender, EventArgs e)
		{
			int id = Convert.ToInt32(dgvCourseInfo.SelectedRows[0].Cells["CourseId"].Value);
			frmUpdateCourse frmUpdateCourse = new frmUpdateCourse(id);
			frmUpdateCourse.ShowDialog();
		}

		private void btnDeleteCourseInfo_Click(object sender, EventArgs e)
		{
			int courseid = Convert.ToInt32(dgvCourseInfo.SelectedRows[0].Cells["CourseId"].Value);
			course_BLL.DeleteCourseInfoById(courseid);
			frmAdmin_Teacher_Load(this, e);
		}

		private void btnInsertStuInfo_Click(object sender, EventArgs e)
		{
			frmAddTeacher frmAddTeacher = new frmAddTeacher();
			frmAddTeacher.FormClosed += FrmAddCourse_FormClosed;
			frmAddTeacher.ShowDialog();

		}

		private void btnUpdateStuInfo_Click(object sender, EventArgs e)
		{
			int id = Convert.ToInt32(dgvStudentInfo.SelectedRows[0].Cells["Id"].Value);
			frmUpdateTeacher frmUpdateTeacher = new frmUpdateTeacher(id);
			frmUpdateTeacher.ShowDialog();
			LoadStudentInfo();
		}

		private void btnDeleteStuInfo_Click(object sender, EventArgs e)
		{
			int studentid = Convert.ToInt32(dgvStudentInfo.SelectedRows[0].Cells["Id"].Value);
			Teacher_BLL.DeleteTeacherInfoById(studentid);
			LoadStudentInfo();
		}

		private void LoadStudentInfo()
		{
			List<TeacherInfo> list = Teacher_BLL.GetTeachers();
			dgvStudentInfo.DataSource = list;
		}

		private void dgvStudentInfo_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
		{

		}

		private void txtSearch_TextChanged(object sender, EventArgs e)
		{
			//只要文本框中的数据，发生了变化，就执行查询操作
			//到底是按照拼音查，还是按照编号查

			//判断用户的输入是啥？
			//用户有没有输入
			if (txtSearch.Text.Length != 0)
			{
				if (txtSearch.Text.All(char.IsDigit))   //0-9
				{
					//按学号搜索
					dgvStudentInfo.DataSource = Teacher_BLL.GetTeacherInfoBySpellOrNum(txtSearch.Text.Trim(), 1);
				}
				else
				{
					//按汉字搜索
					dgvStudentInfo.DataSource = Teacher_BLL.GetTeacherInfoBySpellOrNum(txtSearch.Text.Trim(), 2);
				}
			}
			else
			{
				//展示所有的商品
				LoadStudentInfo();
			}
		}
	}
}
